//下拉框样式
jQuery.fn.selectFilter = function (options){
	var defaults = {
		callBack : function (res){}
	};
	var ops = $.extend({}, defaults, options);
	var selectList = $(this).find('select option');
	var that = this;
	var html = '';
	
	// 读取select 标签的值
	html += '<ul class="filter-list">';
	
	$(selectList).each(function (idx, item){
		var val = $(item).val();
		var valText = $(item).html();
		var selected = $(item).attr('selected');
		var disabled = $(item).attr('disabled');
		var isSelected = selected ? 'filter-selected' : '';
		var isDisabled = disabled ? 'filter-disabled' : '';
		if(selected) {
			html += '<li class="'+ isSelected +'" data-value="'+val+'"><a title="'+valText+'">'+valText+'</a></li>';
			$(that).find('.filter-title').val(valText);
		}else if (disabled){
			html += '<li class="'+ isDisabled +'" data-value="'+val+'"><a>'+valText+'</a></li>';
		}else {
			html += '<li data-value="'+val+'"><a title="'+valText+'">'+valText+'</a></li>';
		};
	});
	
	html += '</ul>';
	$(that).append(html);
	$(that).find('select').hide();
	
	//点击选择
	$(that).on('click', '.filter-text', function (){
		$(that).find('.filter-list').slideToggle(100);
		$(that).find('.filter-list').toggleClass('filter-open');
		$(that).find('.icon-filter-arrow').toggleClass('filter-show');
	});
	
	//点击选择列表
	$(that).find('.filter-list li').not('.filter-disabled').on('click', function (){
		var val = $(this).data('value');
		var valText =  $(this).find('a').html();
		$(that).find('.filter-title').val(valText);
		$(that).find('.icon-filter-arrow').toggleClass('filter-show');
		$(this).addClass('filter-selected').siblings().removeClass('filter-selected');
		$(this).parent().slideToggle(50);
		for(var i=0; i<selectList.length; i++){
			var selectVal = selectList.eq(i).val();
			if(val == selectVal) {
				$(that).find('select').val(val);
			};
		};
		ops.callBack(val); //返回值
	});
	
	//其他元素被点击则收起选择
	$(document).on('mousedown', function(e){
		closeSelect(that, e);
	});
	$(document).on('touchstart', function(e){
		closeSelect(that, e);
	});
	
	function closeSelect(that, e) {
		var filter = $(that).find('.filter-list'),
			filterEl = $(that).find('.filter-list')[0];
		var filterBoxEl = $(that)[0];
		var target = e.target;
		if(filterEl !== target && !$.contains(filterEl, target) && !$.contains(filterBoxEl, target)) {
			filter.slideUp(50);
			$(that).find('.filter-list').removeClass('filter-open');
			$(that).find('.icon-filter-arrow').removeClass('filter-show');
		};
	}
};
$('.filter-box').selectFilter({
    callBack : function (val){
        //返回选择的值
        // console.log(val+'-是返回的值')
    }
});
$('#yy').css({
	height: $(document).height()
  })
  let count=5; //每一页的条数
  let page=1; //当前页
  let maxNum;
//获取总简历人数
$.ajax({
	url:'/zongrenshu',
	success:(res)=>{
		// console.log(res);
		if(res.code==302){
			location.href='/';
			return ;
		}
		if(res.error){
			// console.log(res.error)
		}else{
			let num=res.data.total-res.data2.bufen
			$('.zongk').html('');
			$(res.data).each((i,v)=>{
				$('.zongk').append(`
			<div class="k1">
                <p>全部(${res.data.total})</p>
                <img src="../images/zwgl-1.png">
              </div>
              <div class="k2">
                <p>有新简历投递的职位(${res.data2.bufen})</p>
                <img src="../images/zwgl-2.png">
              </div>
              <div class="k2-f">
                <p>暂无简历投递的职位(${num}})</p>
              </div>
			  `)
			})
	    }
	}
})
//列表
getData();
function xuanranData(c){
	$('.zhaopin').html('');
	$(c).each((i,v)=>{
		let str = `${c[i].j_date}`
		str = str.substring(0,10)
		$.ajax({
			url:'/renshu',
			method:'post',
			data:{
				id:c[i].j_id,
			},
			success:(res)=>{
				if(res.error){
				console.log(res.error)
				}else{
					
					if(res.t1==undefined){
						res.t1={total:0}
						}
						
					$('.zhaopin').append(`
					<div class="ifm clear">
						<div class="zhaopin-1 clear">
						<p class="zhiwu">${v.job_name}</p>
						<p class="xinzi">${v.j_money}</p>
						</div>
						<div class="zhaopin-2 clear">
						<p class="didian">${v.j_address}</p>
						<img src="../images/zwgl-6.png">
						<p class="year">${v.j_year}</p>
						<img src="../images/zwgl-6.png">
						<p class="xueli">${v.j_edu}</p>
						</div>
						<div class="zhaopin-3 clear">
						<img src="${v.c_hrimg}" class="touxiang">
						<p class="xingming">${v.c_hrname}</p>
						<img src="../images/zwgl-6.png">
						<p class="zpzw">招聘HR</p>
						<p class="time">发布于  ${str}</p>
						</div>
						<div class="zhaopin-4">
						<div class="red">
							<span class="num">${res.t1.total}</span>
						</div>
						</div>
						<div class="zhaopin-5 clear">
						<p class="bianji" data-item=${v.j_id} onclick='cunid(this)'><a href="./fabuzhiwei.html">编辑</a></p>
						<p class="dakai" data-item=${v.j_id} onclick='cunid(this)'><a href="./zhiwei.html">打开</a></p>
						<p class="chakan" data-item=${v.j_id} onclick='cunid(this)'><a href="./zhiweixiangqing.html">查看详情</a></p>
						</div>
					</div>
					`)
				}
			}
		})
		
	})
	
}

function xuanranfenye(e){
	$('.page').html('');
	for(let i=1;i<=Math.ceil((e.total/count));i++){
		 if(page==i){
			 $('.page').append(`
			   <a class="pageItem active">${i}</a>
			 `) 
		 }else{
			 $('.page').append(`
			   <a class="pageItem">${i}</a>
			 `) 
		 }
		 maxNum=Math.ceil((e.total/count))
	}
}

    function getData(){
	   $.ajax({
            url:'/getdata',
            data:{
                count:count,
                page:page
            },
            success:(res)=>{
                // console.log(res);
                if(res.code==302){
                    location.href='/';
                    return ;
                }
                if(res.error){
                    // console.log(res.error)
                }else{
                    //列表渲染
					xuanranData(res.data)
                    //页码
					xuanranfenye(res)
					
                }
            }
        })
    }
	//点击页码
    $('.page').on('click','a',function(){
        // console.log($(this).text());
        page=$(this).text();
        getData();
    })
	//点击上一页
	$('.z-arr').click(function(){
		if(page-1>0){
          page=page - 1;
		  getData();
		}
	})
	//点击下一页
	$('.y-arr').click(function(){
		if(page<maxNum){
			page=page+1;
			getData();
		}
	})
//多条件查询
$('.sousuo').click(function(){
    $.ajax({
        url:'/findout',
        method:'post',
        data:{
             time:$('#hk-1 input').val(),zhuangtai:$('.filter-selected').attr('data-value'),name:$('#hk-3 input').val(),count:count,page:page
        },
            success:(res)=>{
				if(res.code==302){
                    location.href='/';
                    return ;
                }
                if(res.error){
                    // console.log(res.error)
                }else{
                    //列表渲染
					xuanranData(res.data)
                    //页码
					xuanranfenye(res)
                }
            }
        })
})
function cunid(obj){
    let id={};
    let str = JSON.stringify($(obj).attr('data-item'));
    sessionStorage.setItem("id",str)
  }